﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Enum;


public partial class TotalDrugs_OrderStatistics : BasePage
{
    public TotalDrugs_OrderStatistics()
    {
        this.ModuleNo = Module.医院统计分析.ToString();
        this.PageOperateType = OperateType.医院采购单统计.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);
            BindData();//绑定数据
            QueryControl1.BindSort(gvwInfo);
            GetOrderCount();
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
        GetOrderCount();
    }

    protected void BindData()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.OrderID,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseAmountTotal : ShowColumns.ShowColumn.GroupFields,
            SortField = QueryControl1.SortField == "" ? "PlanTime ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);

        AspNetPager1.RecordCount = t;
        gvwInfo.PageSize = AspNetPager1.PageSize;
        gvwInfo.AutoGenerateColumns = false;
        GridViewUtil.AdjustGridView(gvwInfo, dt);
        gvwInfo.DataSource = dt;
        gvwInfo.DataBind();
        setChart(dt);
        SetColumnVisible();
    }



    protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //按企业查看
        if (e.CommandName == "DirectByCom")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //query.OrderID = e.CommandArgument.ToString();
            string[] Order = e.CommandArgument.ToString().Split(',');
            query.OrderID = Order[0].ToString();
            query.OrderName = Order[1].ToString();
            query._QueryPrompt = query._QueryPrompt + " " + Order[1].ToString();
            query._Titile = "采购单相关配送企业";

            Session[sessionid] = query;

            string url = System.Configuration.ConfigurationManager.AppSettings["CompanyID_PS"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));

        }

        //按药品查看
        if (e.CommandName == "DierectByDrug")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal;
            }


            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //query.OrderID = e.CommandArgument.ToString();
            string[] Order = e.CommandArgument.ToString().Split(',');
            query.OrderID = Order[0].ToString();
            query.OrderName = Order[1].ToString();
            query._QueryPrompt = query._QueryPrompt + " " + Order[1].ToString();
            query._Titile = "采购单相关药品";

            Session[sessionid] = query;

            string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        if (e.CommandName == "DierectByDetail")
        {
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.PurchaseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.WareHouseRate;

            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //query.OrderID = e.CommandArgument.ToString();
            string[] Order = e.CommandArgument.ToString().Split(',');
            query.OrderID = Order[0].ToString();
            query.OrderName = Order[1].ToString();
            query._QueryPrompt = query._QueryPrompt + " " + Order[1].ToString();


            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HosComDrugs"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }


    #region   数据准备



    /// <summary>
    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {
        gvwInfo.Columns[1].Visible = false;
        if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnDrug").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnCom_PS").Visible = false;
            }
        }
        //if (!string.IsNullOrEmpty(txtHospitalId.Value))
        //{
        //    foreach (GridViewRow row in gvwInfo.Rows)
        //    {
        //        row.FindControl("lbnHos").Visible = false;
        //    }
        //}
    }

    #endregion
    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;
        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //query.OrderID = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
            //query.OrderName=row.Cells[3].Text;
            query.OrderID = gvwInfo.DataKeys[row.RowIndex].Values["OrderID"].ToString();
            query.OrderName = gvwInfo.DataKeys[row.RowIndex].Values["OrderName"].ToString();
            query._QueryPrompt = query._QueryPrompt + " " + gvwInfo.DataKeys[row.RowIndex].Values["OrderName"].ToString();

            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "订单";
            serChart.DataSource = dt;

            if (ShowColumns.ShowColumn == null)
            {
                serChart.XName = "OrderName";
                serChart.YNames = "PurchaseAmountTotal";
            }
            else
            {
                string[] _columns = ShowColumns.ShowColumn.GroupFields.ToString().Split(',');
                serChart.XName = "OrderName";
                serChart.YNames = _columns[0];
            }

            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

            ChartControl1.webChart = serChart;
            ChartControl1.PageId = Guid.NewGuid().ToString();
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.OrderID,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseAmountTotal : ShowColumns.ShowColumn.GroupFields,
            SortField = QueryControl1.SortField == "" ? "OrderID ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(0, 0, out t);

        // GridViewUtil.AdjustGridView(gvwInfo, dt);
        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
    public void GetOrderCount()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.OrderID,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseAmountTotal : ShowColumns.ShowColumn.GroupFields,
            SortField = QueryControl1.SortField == "" ? "PlanTime ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(1,0, out t);
        double PurchaseAmountTotal = 0;
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PurchaseAmountTotal += HZ.MathUtil.SToF(dt.Rows[i]["PurchaseAmountTotal"].ToString());
            }
            lblCount.Text = "采购总金额为:" + PurchaseAmountTotal.ToString("N2") + "";
        }
        else
        {
            lblCount.Text = "暂无符合条件的数据！";
        }
    }
}
